package com.unohacha.qqj.fzdr.login;

import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.imp.MyCallBack;
import com.unohacha.qqj.fzdr.imp.UrlConfig;
import com.unohacha.qqj.fzdr.imp.XUtil;
import com.unohacha.qqj.fzdr.util.CountDownTimerUtils;
import com.unohacha.qqj.fzdr.view.ClearEditText;
import com.unohacha.qqj.mylibrary.base.BaseActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 注册
 */
public class RegisterActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {

    @BindView(R.id.common_back)
    ImageView commonBack;
    @BindView(R.id.common_title)
    TextView commonTitle;
    @BindView(R.id.common_right_image)
    ImageView commonRightImage;
    @BindView(R.id.common_right_text)
    TextView commonRightText;
    @BindView(R.id.register_username)
    ClearEditText registerUsername;
    @BindView(R.id.register_code)
    ClearEditText registerCode;
    @BindView(R.id.register_send_code)
    TextView registerSendCode;
    @BindView(R.id.register_password)
    ClearEditText registerPassword;
    @BindView(R.id.register_hide_password)
    CheckBox registerHidePassword;
    @BindView(R.id.register_service_agreement)
    ImageView registerServiceAgreement;
    @BindView(R.id.register)
    Button register;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_register_layout;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setVisibility(View.INVISIBLE);
        commonRightImage.setVisibility(View.INVISIBLE);
        registerHidePassword.setOnCheckedChangeListener(this);

    }

    @OnClick({R.id.common_back, R.id.register_username, R.id.register_code, R.id.register_send_code, R.id.register_password, R.id.register_service_agreement, R.id.register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                finish();
                break;
            case R.id.register_username://注册账号

                break;
            case R.id.register_code://验证码

                break;
            case R.id.register_send_code://注册验证码

                if (TextUtils.isEmpty(registerUsername.getText().toString().trim())) {
                    MyToast(getResources().getString(R.string.text_user_phone));
                    return;
                }
                registerVercodeFunc(1);
                break;
            case R.id.register_password://注册密码

                break;
            case R.id.register_service_agreement:
                break;
            case R.id.register://注册

                if (TextUtils.isEmpty(registerUsername.getText().toString().trim())) {
                    MyToast(getResources().getString(R.string.text_user_phone));
                    return;
                }

                if (TextUtils.isEmpty(registerPassword.getText().toString().trim())) {
                    MyToast(getResources().getString(R.string.text_user_pwd));
                    return;
                }

                if (TextUtils.isEmpty(registerCode.getText().toString().trim())) {
                    MyToast(getResources().getString(R.string.text_code));
                    return;
                }

                registerFunc(registerUsername.getText().toString().trim(), registerPassword.getText().toString().trim(),
                        registerCode.getText().toString().trim());

                break;
        }

    }


    //注册验证码 typeid  1注册 2-找回密码 3-修改密码 4-改新手机
    private void registerVercodeFunc(int typeid) {
        Map<String, Object> params = new HashMap<>();
        params.put("typeid", typeid);//1注册 2-找回密码 3-修改密码 4-改新手机
        params.put("mobile", registerUsername.getText().toString().trim());
        XUtil.Post(UrlConfig.REGISTER_CODE, params, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if ("1".equals(jsonObject.optString("re"))) {

                        CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(registerSendCode, 60000, 1000);
                        mCountDownTimerUtils.start();
                    }
                    MyToast(jsonObject.optString("info"));

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    //注册
    private void registerFunc(String telephone, String password, String verifyCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("telephone", telephone);
        params.put("password", password);
        params.put("verifyCode", verifyCode);
        XUtil.Post(UrlConfig.REGISTER_FUNC, params, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    MyToast(jsonObject.optString("message"));
                    if ("1".equals(jsonObject.optString("code"))) {
                        jumpToOtherActivity(RegisterActivity.this, LoginActivity.class);
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);

            }
        });
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        if (isChecked == true) {
            registerPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        } else {
            registerPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }
        registerPassword.setSelection(registerPassword.getText().toString().length());
    }
}
